package com.netty.chapter05;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoop loop = new NioEventLoopGroup().next();
        DefaultPromise<Integer> promise=new DefaultPromise<>(loop);
        new Thread(()->{
            log.info("开始执行...");
            try {
                int a=1/0;
                int res=10;
                Thread.sleep(1000);
                promise.setSuccess(res);
            }catch (Exception e){
                promise.setFailure(e);
            }
        }).start();
        log.info("等待结果。。。");
        try {
            log.info("结果为:{}",promise.get());
        }catch (Exception e){
            log.info("异常发生了..........................");
            e.printStackTrace();
        }
    }
}
